using JT808Protocol.Common;
using JT808Protocol.Models.BodyParserStrategy.DeviceParams;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy
{
    /// <summary>
    /// 查询终端参数应答
    /// </summary>
    public class DpDeviceParamQuery_0x0104 : IJT808Body
    {
        /// <summary>
        /// 应答流水号
        /// </summary>
        [JsonPropertyName("应答流水号")] public ushort ReqSn { get; set; }

        /// <summary>
        /// 参数个数
        /// </summary>
        [JsonPropertyName("参数个数")] public byte ParamCount { get; set; }

        /// <summary>
        /// 参数项列表
        /// </summary>
        [JsonPropertyName("参数项列表")] public List<IDeviceParam> ParamList { get; private set; }

        public override IJT808Body ReadBody(ReadOnlySpan<byte> source, ref int index)
        {
            ReqSn = ParseHelper.Uint16(ref source, index);

            index += 2;
            ParamCount = source[index];

            ParamList = new List<IDeviceParam>();

            index++;
            for (int i = 0; i < ParamCount; i++)
            {
                var paramIDValue = ParseHelper.Uint32(ref source, index);

                IDeviceParam param = null;

                switch (paramIDValue)
                {
                    case 0x01:
                    case 0x02:
                    case 0x03:
                    case 0x04:
                    case 0x05:
                    case 0x06:
                    case 0x07:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x10:
                    case 0x11:
                    case 0x12:
                    case 0x13:
                    case 0x14:
                    case 0x15:
                    case 0x16:
                    case 0x17:
                    case 0x1A:
                        param = new DpString(paramIDValue);
                        break;

                    case 0x1B:
                    case 0x1C:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x1D:
                        param = new DpString(paramIDValue);
                        break;

                    case 0x20:
                    case 0x21:
                    case 0x22:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x23:
                    case 0x24:
                    case 0x25:
                    case 0x26:
                        param = new DpString(paramIDValue);
                        break;

                    case 0x27:
                    case 0x28:
                    case 0x29:
                    case 0x2C:
                    case 0x2D:
                    case 0x2E:
                    case 0x2F:
                    case 0x30:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x31:
                        param = new DpWordUshort(paramIDValue);
                        break;

                    case 0x32:
                        param = new DpDeregulationTime_0x32();
                        break;

                    case 0x40:
                    case 0x41:
                    case 0x42:
                    case 0x43:
                    case 0x44:
                        param = new DpString(paramIDValue);
                        break;

                    case 0x45:
                    case 0x46:
                    case 0x47:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x48:
                    case 0x49:
                        param = new DpString(paramIDValue);
                        break;

                    case 0x50:
                    case 0x51:
                    case 0x52:
                    case 0x53:
                    case 0x54:
                    case 0x55:
                    case 0x56:
                    case 0x57:
                    case 0x58:
                    case 0x59:
                    case 0x5A:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x5B:
                    case 0x5C:
                    case 0x5D:
                    case 0x5E:
                        param = new DpWordUshort(paramIDValue);
                        break;

                    case 0x64:
                        break;

                    case 0x65:
                        break;

                    case 0x70:
                    case 0x71:
                    case 0x72:
                    case 0x73:
                    case 0x74:
                    case 0x80:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    #region 1078-2016 中新增的参数

                    case 0x75:
                        param = new DpAv_0x75();
                        break;

                    case 0x76:
                        param = new DpAvChannelParam_0x76();
                        break;

                    case 0x77:
                        param = new DpAvSingleParam_0x77();
                        break;

                    case 0x79:
                        param = new DpSpecialAlarmAvParam_0x79();
                        break;

                    case 0x7A:
                        param = new DpVideoShieldAlarm_0x7A();
                        break;

                    case 0x7B:
                        param = new DpImageAnalyseAlarm_0x7B();
                        break;

                    case 0x7C:
                        param = new DpDeviceWakeUpMod_0x7C();
                        break;

                    #endregion 1078-2016 中新增的参数

                    case 0x81:
                    case 0x82:
                        param = new DpWordUshort(paramIDValue);
                        break;

                    case 0x83:
                        param = new DpString(paramIDValue);
                        break;

                    case 0x84:
                    case 0x90:
                    case 0x91:
                    case 0x92:
                        param = new DpByte(paramIDValue);
                        break;

                    case 0x093:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x94:
                        param = new DpByte(paramIDValue);
                        break;

                    case 0x95:
                    case 0x100:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x101:
                        param = new DpWordUshort(paramIDValue);
                        break;

                    case 0x102:
                        param = new DpDwordUint(paramIDValue);
                        break;

                    case 0x103:
                        param = new DpWordUshort(paramIDValue);
                        break;

                    case 0x110:
                        param = new DpCanSet_0x110();
                        break;

                    default:
                        break;
                }

                if (param != null)
                {
                    param.ReadEntity(ref source,ref index);
                    ParamList.Add(param);
                }
                else
                {
                    index += (ushort)(5 + source[index + 4]);
                }
            }

            return this;
        }

        public override List<byte> WriteToBytes()
        {
            throw new System.NotImplementedException();
        }
    }
}